package com.conan.service.Impl;

import com.conan.mapper.CategoryMapper;
import com.conan.mapper.UserMapper;
import com.conan.pojo.Category;
import com.conan.service.CategoryService;
import com.conan.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;


    //添加文章
    @Override
    public void addCategory(Category category) {

        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());

        Map<String,Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        category.setCreateUser(id);

        categoryMapper.addCategory(category);
    }
    //根据用户id获取文章列表
    @Override
    public List<Category> getCategoryList() {
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        return categoryMapper.getCategoryByUserId(userId);
    }
    //根据文章id获取文章详细信息
    @Override
    public Category getCategoryById(Integer id) {
        return categoryMapper.getCategoryById(id);
    }

    //更新文章信息
    @Override
    public void updateCategory(Category category) {
        category.setUpdateTime(LocalDateTime.now());
        categoryMapper.updateCategory(category);
    }
    //删除文章
    @Override
    public void deleteCategory(Integer id) {
        categoryMapper.deleteCategoryById(id);
    }
}
